/*
 * Copyright (c) 2019,TravelSky.
 * All Rights Reserved.
 * TravelSky CONFIDENTIAL
 *
 */

package cn.yangliu.jpa.base.service;

import java.io.Serializable;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.lang.Nullable;
import org.springframework.transaction.TransactionStatus;

/**
 * The interface Jpa service.
 *
 * @param <T>  the type parameter
 * @param <ID> the type parameter
 * @author 问道于盲
 * @date 2019 -06-11 01:22
 */
public interface JpaService<T, ID extends Serializable> {

    /**
     * Execute t.
     *
     * @param function the function
     * @return the t
     */
    T execute(Function<TransactionStatus, T> function);

    /**
     * Find all list.
     *
     * @return the list
     */
    List<T> findAll();

    /**
     * Find all list.
     *
     * @param sort the sort
     * @return the list
     */
    List<T> findAll(Sort sort);

    /**
     * Find all page.
     *
     * @param pageable the pageable
     * @return the page
     */
    Page<T> findAll(Pageable pageable);

    /**
     * Find all by id list.
     *
     * @param ids the ids
     * @return the list
     */
    List<T> findAllById(Iterable<ID> ids);

    /**
     * Count long.
     *
     * @return the long
     */
    long count();

    /**
     * Delete by id.
     *
     * @param id the id
     */
    void deleteById(ID id);

    /**
     * Delete.
     *
     * @param entity the entity
     */
    void delete(T entity);

    /**
     * Delete all.
     *
     * @param iterable the iterable
     */
    void deleteAll(Iterable<? extends T> iterable);

    /**
     * Delete all.
     */
    void deleteAll();

    /**
     * Save t.
     *
     * @param entity the entity
     * @return the t
     */
    T save(T entity);

    /**
     * Save all list.
     *
     * @param entities the entities
     * @return the list
     */
    List<T> saveAll(Iterable<T> entities);

    /**
     * Find by id optional.
     *
     * @param id the id
     * @return the optional
     */
    Optional<T> findById(ID id);

    /**
     * Exists by id boolean.
     *
     * @param id the id
     * @return the boolean
     */
    boolean existsById(ID id);

    /**
     * Save and flush t.
     *
     * @param entity the entity
     * @return the t
     */
    T saveAndFlush(T entity);

    /**
     * Delete in batch.
     *
     * @param entities the entities
     */
    void deleteInBatch(Iterable<T> entities);

    /**
     * Delete all in batch.
     */
    void deleteAllInBatch();

    /**
     * current class method for getter.
     * Gets one.
     *
     * @param id the id
     * @return the one
     */
    T getOne(ID id);

    /**
     * Find one optional.
     *
     * @param queryPojo the query pojo
     * @return the optional
     */
    Optional<T> findOne(T queryPojo);

    /**
     * Find all iterable.
     *
     * @param queryPojo the query pojo
     * @return the iterable
     */
    Iterable<T> findAll(T queryPojo);

    /**
     * Find all iterable.
     *
     * @param queryPojo the query pojo
     * @param sort      the sort
     * @return the iterable
     */
    Iterable<T> findAll(T queryPojo, Sort sort);

    /**
     * Find all page.
     *
     * @param queryPojo the query pojo
     * @param pageable  the pageable
     * @return the page
     */
    Page<T> findAll(T queryPojo, Pageable pageable);

    /**
     * Count long.
     *
     * @param queryPojo the query pojo
     * @return the long
     */
    long count(T queryPojo);

    /**
     * Exists boolean.
     *
     * @param queryPojo the query pojo
     * @return the boolean
     */
    boolean exists(T queryPojo);

    /**
     * Find one optional.
     *
     * @param spec the spec
     * @return the optional
     */
    Optional<T> findOne(@Nullable Specification<T> spec);

    /**
     * Find all list.
     *
     * @param spec the spec
     * @return the list
     */
    List<T> findAll(@Nullable Specification<T> spec);

    /**
     * Find all page.
     *
     * @param spec     the spec
     * @param pageable the pageable
     * @return the page
     */
    Page<T> findAll(@Nullable Specification<T> spec, Pageable pageable);

    /**
     * Find all list.
     *
     * @param spec the spec
     * @param sort the sort
     * @return the list
     */
    List<T> findAll(@Nullable Specification<T> spec, Sort sort);

    /**
     * Count long.
     *
     * @param spec the spec
     * @return the long
     */
    long count(@Nullable Specification<T> spec);

}
